package com.mdh.runerrands.controller;

import com.alibaba.druid.util.StringUtils;
import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.BaseUser;
import com.mdh.runerrands.pojo.QueryPage;
import com.mdh.runerrands.service.BaseUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/controller/customer")
public class CustomController {
    @Resource
    private BaseUserService baseUserService;
    @ApiOperation(value = "查询所有顾客信息")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findCustomerAll(){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            List<BaseUser> baseUser = baseUserService.findBaseUserAll();
            List<BaseUser> customer = new ArrayList<>();
            for (BaseUser s : baseUser){
                if ("customer".equals(s.getType())){
                    customer.add(s);
                }
            }
            result.setData(customer);
            result.setStatus("200");
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setMessage("false");
            result.setStatus("500");
            result.setData(e.getMessage());
            return result;
        }
    }
    @ApiOperation(value = "通过id删除顾客信息")
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    public Result deleteCustomerById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            int i = baseUserService.deleteBaseUserById(id);
            if (i>0){
                result.setStatus("200");
                result.setMessage("success");
                result.setData(i);
                return result;
            }
            result.setMessage("要删除的用户不存在");
            result.setData(null);
            result.setStatus("200");
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("false");
            return result;
        }
    }
    @ApiOperation(value = "顾客注册")
    @RequestMapping(value = "/customerRegister", method = RequestMethod.POST)
    public Result customerRegister(BaseUser baseUser){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            int insert = baseUserService.insert(baseUser);
            if (insert > 0){
                result.setMessage("success");
                result.setData(insert);
                return result;
            }
            result.setData(null);
            result.setMessage("注册用户失败");
            return result;
        }catch (Exception e){
            result.setMessage("false");
            result.setStatus("500");
            result.setData(e.getMessage());
            return result;
        }
    }
    @ApiOperation(value = "通过id查找顾客信息")
    @RequestMapping(value = "/findCustomerById",method = RequestMethod.GET)
    public Result findCustomerById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            BaseUser user = baseUserService.findBaseUserById(id);
            if (user!=null && "customer".equals(user.getType())){
            result.setData(user);
            result.setMessage("success");
            return result;
            }
            result.setData(null);
            result.setMessage("查找的用户不存在");
            return result;
        }catch (Exception e){
            result.setMessage("false");
            result.setData(e.getMessage());
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation(value = "保存或者更新顾客信息")
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Result saveOrUpdate(BaseUser baseUser){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            if (baseUser.getId() == 0){
                int insert = baseUserService.insert(baseUser);
                if (insert > 0) {
                    result.setData(insert);
                    result.setMessage("success");
                    return result;
                }
                result.setData(null);
                result.setMessage("保存用户失败");
                return result;
            }else {
                int update = baseUserService.update(baseUser);
                if (update > 0) {
                    result.setData(update);
                    result.setMessage("success");
                    return result;
                }
                result.setData(null);
                result.setMessage("更新用户信息失败");
                return result;
            }
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }

    @ApiOperation("批量删除顾客信息")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int ...ids){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = baseUserService.batchDelete(ids);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation("分页查询顾客信息")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Result query(int page ,int pageSize,String realname,String telephone,String status){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            if (StringUtils.isEmpty(realname)){
                realname = null;
            }else if (StringUtils.isEmpty(telephone)){
                telephone = null;
            }else if (StringUtils.isEmpty(status)){
                status = null;
            }
            QueryPage queryPage = new QueryPage();
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            queryPage.setList(baseUserService.customerQuery(page,pageSize,realname,telephone,status));
            queryPage.setTotal(baseUserService.customerCount(realname,telephone,status));
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }

    @ApiOperation("顾客充值")
    @RequestMapping(value = "/recharge" ,method = RequestMethod.GET)
    public Result recharge(int id ,String money){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean recharge = baseUserService.recharge(id, money);
            result.setData(recharge);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }
}
